(function(){

	//常用函数合集
	window.prideCore = {
		iconClass:iconClass,
		slides:slides,
		dragging:dragging,
		tabs:tabs,
		accordion:accordion,
		accordionX:accordionX,
		cityToggle:cityToggle
	}




	/**
	 * 分类列表滑动
	 * obj:容器(必须)
	 **/
	function iconClass(obj){
		var container = $(obj),
				box = container.children(),
				cell = box.find('li'),
				len = cell.length,
				ww = $(window).width(),
				countX = 0,
				index = 0,
				direction = 1,
				isMove = false;


		//初始化容器宽高
		containerCount();


		//窗口变化时重新计算容器宽高
		$(window).resize(containerCount);


		container.append('<div class="slide-page"></div>');

		var slidePage = $('.slide-page');


		//输出圆点
		cell.each(function(index){
			if(index === 0){
				slidePage.append('<i class="on"></i>');
			}else{
				slidePage.append('<i></i>');
			}
		});

		var icon = slidePage.children(),
				node = box.children(),
				nodeA = node.first().clone(),
				nodeB = node.last().clone();


		//圆点水平居中
		slidePage.css({margin:'0 0 0 -'+slidePage.width()/2+'px'});


		//初始化图片排序
		box.append(nodeA).prepend(nodeB).css({left:-ww});


		//手指按住屏幕滑动
		box.on('touchstart',function(event){
			var startX = event.originalEvent.targetTouches[0].pageX;

			box.on('touchmove',function(event){
				var pageX = event.originalEvent.targetTouches[0].pageX;

					countX = pageX-startX;
					isMove = true;
					box.css({left:countX-ww});
			});

		});


		//手指离开时判断坐标
		box.on('touchend',function(){
			if(!isMove){
				return;
			}

			if(Math.abs(countX) > ww/3){

				if(countX < 0){
					index = (index < len-1) ? index+1 : 0;
					direction = 1;
					box.animate({left:-(ww*2)},200,imgRealign);
				}else{
					index = (index > 0) ? index-1 : len-1;
					direction = 2;
					box.animate({left:0},200,imgRealign);
				}

			}else{
				box.animate({left:-ww},200);
			}

			isMove = false;
			box.off('touchmove');
		});




		//重新调整图片排序
		function imgRealign(){
			box.children().first().remove();
			box.children().last().remove();

			if(direction === 1){
				box.children().first().appendTo(box);
			}else{
				box.children().last().prependTo(box);
			}

			node = box.children(),
			nodeA = node.first().clone(),
			nodeB = node.last().clone();

			box.append(nodeA).prepend(nodeB).css({left:-ww});
			icon.removeClass('on').eq(index).addClass('on');
		}


		//计算容器宽高
		function containerCount(){
			ww = $(window).width();

			cell.width(ww);

			var imgHeight = cell.height();

			container.height(imgHeight);
			box.width(ww*len*3).height(imgHeight);
		}


	}




	/**
	 * 无缝轮番
	 * obj:参数合集
	 *   id:容器Id(必须)
	 *   isAuto:是否自动(可选)
	 *   interval:间隔时间(可选)
	 **/
	function slides(obj){
		var container = $(obj.id),
				box = container.children(),
				isAuto = obj.isAuto || false,
				interval = obj.interval || 3000,
				img = box.find('img'),
				len = img.length,
				ww = $(window).width(),
				countX = 0,
				index = 0,
				direction = 1,
				isMove = false;


		//初始化容器宽高
		containerCount();


		//窗口变化时重新计算容器宽高
		$(window).resize(containerCount);


		container.append('<div class="slide-page"></div>');

		var slidePage = $('.slide-page');


		//输出圆点
		img.each(function(index){
			if(index === 0){
				slidePage.append('<i class="on"></i>');
			}else{
				slidePage.append('<i></i>');
			}
		});

		var icon = slidePage.children(),
				node = box.children(),
				nodeA = node.first().clone(),
				nodeB = node.last().clone();


		//圆点水平居中
		slidePage.css({margin:'0 0 0 -'+slidePage.width()/2+'px'});


		//初始化图片排序
		box.append(nodeA).prepend(nodeB).css({left:-ww});


		//自动轮番
		if(isAuto){
			setInterval(function(){
				index = (index < len-1) ? index+1 : 0;
				direction = 1;
				box.animate({left:-(ww*2)},200,imgRealign);
			},interval);
		}


		//手指按住屏幕滑动
		box.on('touchstart',function(event){
			var startX = event.originalEvent.targetTouches[0].pageX;

			box.on('touchmove',function(event){
				var pageX = event.originalEvent.targetTouches[0].pageX;

					countX = pageX-startX;
					isMove = true;
					box.css({left:countX-ww});
			});

		});


		//手指离开时判断坐标
		box.on('touchend',function(){
			if(!isMove){
				return;
			}

			if(Math.abs(countX) > ww/3){

				if(countX < 0){
					index = (index < len-1) ? index+1 : 0;
					direction = 1;
					box.animate({left:-(ww*2)},200,imgRealign);
				}else{
					index = (index > 0) ? index-1 : len-1;
					direction = 2;
					box.animate({left:0},200,imgRealign);
				}

			}else{
				box.animate({left:-ww},200);
			}

			isMove = false;
			box.off('touchmove');
		});




		//重新调整图片排序
		function imgRealign(){
			box.children().first().remove();
			box.children().last().remove();

			if(direction === 1){
				box.children().first().appendTo(box);
			}else{
				box.children().last().prependTo(box);
			}

			node = box.children(),
			nodeA = node.first().clone(),
			nodeB = node.last().clone();

			box.append(nodeA).prepend(nodeB).css({left:-ww});
			icon.removeClass('on').eq(index).addClass('on');
		}


		//计算容器宽高
		function containerCount(){
			ww = $(window).width();

			img.width(ww);

			var imgHeight = img.height();

			container.height(imgHeight);
			box.width(ww*len*3).height(imgHeight);
		}


	}



	/**
	 * 手指左右拖拽
	  * obj:参数合集
	 *   id:拖拽容器Id
	 * 	 margin:每个元素的间距
	 *   show:当前可见元素个数
	 **/
	function dragging(obj){
		var box = $(obj.id),
				margin = obj.margin,
				shows = obj.shows,
				item = box.children(),
				len = item.length,
				itemW = item.width(),
				boxW = (itemW+margin)*len+margin;

			item.width(itemW);
			box.width(boxW);

			//超出可见个数才绑定拖拽
			if(len < shows){
				return;
			}


			//绑定拖拽效果
			box.on('touchstart',function(event){
				var startX = event.originalEvent.targetTouches[0].pageX,
						offsetLeft = box.offset().left;

				box.on('touchmove',function(event){
					var pageX = event.originalEvent.targetTouches[0].pageX,
							ww = boxW-$(window).width(),
							count = offsetLeft+pageX-startX;

					//左右到顶锁住拖拽
					if(count > 0){
						box.css({left:0});
					}else{
						if(Math.abs(count) > ww){
							box.css({left:-ww});
						}else{
							box.css({left:count});
						}
					}

				});

			});


			box.on('touchend',function(){
				box.off('touchmove');
			});

	}




	/**
	 * 选项卡
	 * obj:参数合集
	 *   tab:选项卡按钮容器
	 * 	 container:选项卡切换容器
	 *   style:按钮高亮样式名
	 **/
	function tabs(obj){
	 	var tab = $(obj.tab),
	 			container = $(obj.container),
	 			button = tab.children(),
	 			box = container.children(),
	 			style = obj.style;

	 	box.hide().first().show();

	 	button.on('click',function(){
	 		var index = $(this).index();

	 		button.removeClass(style).eq(index).addClass(style);
	 		box.hide().eq(index).show();
	 	});
	}



	/**
	 * 单个手风琴效果
	 * obj:参数合集
	 *   button:手风琴按钮
	 * 	 container:手风琴容器
	 *   height:容器初始显示高度
	 *   classA:按下前样式名
	 *   classB:按下后样式名
	 **/
	function accordion(obj){
		var button = $(obj.button),
				container = $(obj.container),
				height = obj.height,
				classA = obj.classA,
				classB = obj.classB;

		button.on('click',function(){
			if($(this).hasClass(classA)){
				$(this).addClass(classB).removeClass(classA);
				container.css({height:'auto'});
			}else{
				$(this).addClass(classA).removeClass(classB);
				container.css({height:height+'px'});
			}

		});

	}



	/**
	 * 多个手风琴效果
	 * obj:参数合集
	 * 	 container:手风琴容器
	 *   height:容器初始显示高度
	 *   classA:按下前样式名
	 *   classB:按下后样式名
	 **/
	function accordionX(obj){
		var container = $(obj.container),
				height = obj.height,
				classA = obj.classA,
				classB = obj.classB;

		container.on('click','i',function(){
			var box = $(this).parent().siblings();

			if($(this).hasClass(classA)){
				$(this).addClass(classB).removeClass(classA);
				box.css({height:'auto'});
			}else{
				$(this).addClass(classA).removeClass(classB);
				box.css({height:height+'px'});
			}

		});

	}



	/**
	 * 多个下拉菜单
	 * obj:参数合集
	 *   button:按钮
	 * 	 container:可见容器
	 *   classA:按下前样式名
	 *   classB:按下后样式名
	 **/
	function cityToggle(obj){
	 	var button = $(obj.button),
	 			container = $(obj.container),
	 			classA = obj.classA,
	 			classB = obj.classB;

	 	button.children().on('click',function(){
	 		var index = $(this).index(),
	 				thisSub = $(this).children(),
	 				other = $(this).siblings(),
	 				otherSub = other.children();

	 		if($(this).hasClass('on')){
	 			$(this).removeClass('on');
	 			thisSub.removeClass(classB).addClass(classA);
	 			container.eq(index).slideUp(100);
	 		}else{
	 			$(this).addClass('on');
	 			other.removeClass('on');
	 			otherSub.removeClass(classB).addClass(classA);
	 			thisSub.removeClass(classA).addClass(classB);
	 			container.hide().eq(index).slideDown(100);
	 		}
	 	});

	}




})();